package components
{
	import UIFree.uif_bitmap;
	import UIFree.uif_component;
	import UIFree.uif_loader;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	public class c_image extends uif_bitmap {
		private var _source: Object;
		public function c_image(parent:uif_component, a_source: Object=null) {
			super();
			source = a_source;
			parent.addChild(this);
		}
		
		public function set source(a_source: Object): void {
			if (_source != a_source) setSource(a_source);
		}
		
		public function get source(): Object {
			return _source;
		}
		
		protected function setSource(a_source: Object): void {
			if (a_source is String) setAsString(a_source as String);
			else if (a_source is BitmapData) setAsBitmapData(a_source as BitmapData);
		}
		
		private function setAsString(a_source: String): void {
			var _loader: uif_loader = new uif_loader(null, a_source, function(e: Event): void {
				bitmapData = new BitmapData(_loader.contentLoaderInfo.width, _loader.contentLoaderInfo.height, true, 0);
				bitmapData.draw(_loader);
				doComplete();
			});
		}
		
		private function setAsBitmapData(a_source: BitmapData): void {
			bitmapData = a_source.clone();
			doComplete();
		}
		
		protected function doComplete(): void {
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}